/*
 * These files are distributed under the terms of the GNU GPLv2, the GNU General
 * Public version 2. See LICENSE.txt
 */
package fileCommodore.plugin.usermenu;

import java.awt.*;
import java.util.*;

import javax.swing.*;

public class EntryEditorPanel extends JPanel
{
	private JComboBox type = new JComboBox( EntryType.values() );
	
	private JTextField fName = new JTextField("Name");
	private JTextField fProgram = new JTextField("Program");
	private JTextField fArgs = new JTextField("Arguments");
	
	private JLabel lName = new JLabel("Name", JLabel.RIGHT);
	private JLabel lProgram = new JLabel("Program", JLabel.RIGHT);
	private JLabel lArgs = new JLabel("Arguments", JLabel.RIGHT);
	
	private JTextArea help = new JTextArea();
	private JScrollPane helpPane = new JScrollPane(help);
	
	private Entry entry;

	private GridBagConstraints gbc = new GridBagConstraints();
	private GridBagLayout gbl = new GridBagLayout();
	
	public EntryEditorPanel(Entry entry)
	{
		setEntry(entry);
		
		Vector<String> h = new Vector<String>();
		h.add("Special Variables");
		h.add("");
		h.add("\t%d\tActive Panel's Directory");
		h.add("\t%D\tInactive Panel's Directory");
		h.add("");
		h.add("\t%s\tActive Panel's Selected File");
		h.add("\t%S\tInactive Panel's Selcted File");
		h.add("");
		h.add("\t%n\tActive Panel's Selected File without Path");
		h.add("\t%N\tInactive Panel's Selcted File without Path");
		h.add("");
		h.add("\t%t\tActive Panel's Tagged Files");
		h.add("\t%T\tInactive Panel's Tagged Files");
		h.add("");
		h.add("\t@t\tActive Panel's Tagged Files");
		h.add("\t@T\tInactive Panel's Tagged Files");
		h.add("");
		h.add("%t will insert all the tagged files on the same command.");
		h.add("@t will loop over the tagged files and execute with a single one at a time.");
		StringBuilder sbHelp = new StringBuilder();
		for(String s: h)
		{
			sbHelp.append(s);
			sbHelp.append("\r\n");
		}
		help.setTabSize(4);
		help.setText(sbHelp.toString());
		help.setEditable(false);
		
		setLayout(gbl);

		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.gridwidth = 2;
		gbc.gridheight = 1;
		gbc.weightx = 1;
		gbc.weighty = 0;
		gbc.insets = new Insets(5, 5, 5, 5);
		gbc.fill = GridBagConstraints.NONE;
		add(type, gbc);
		
		gbc.gridwidth = 1;
		
		gbc.fill = GridBagConstraints.BOTH;
		gbc.weightx = 0;
		gbc.gridx = 0;
		gbc.gridy = 1;
		add(lName, gbc);
		gbc.gridy = 2;
		add(lProgram, gbc);
		gbc.gridy = 3;
		add(lArgs, gbc);
		
		gbc.gridx = 1;
		gbc.gridy = 1;
		add(fName, gbc);
		gbc.gridy = 2;
		add(fProgram, gbc);
		gbc.gridy = 3;
		add(fArgs, gbc);
		
		gbc.gridwidth = 2;
		gbc.gridx = 0;
		gbc.gridy = 4;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.weighty = 1;
		add(helpPane, gbc);
		
		load();
	}

	public Entry getEntry() {
		return entry;
	}

	public void setEntry(Entry entry) {
		this.entry = entry;
	}
	
	public void save()
	{
		entry.setType( (EntryType) type.getSelectedItem());
		entry.setName(fName.getText());
		entry.setProgram(fProgram.getText());
		entry.setArguments(fArgs.getText());
	}
	
	private void load()
	{
		type.setSelectedItem(getEntry().getType());
		fName.setText(getEntry().getName());
		fProgram.setText(getEntry().getProgram());
		fArgs.setText(getEntry().getArguments());
	}
}
